<?php

if(($block = $this->getLayout()->getBlock('costpriceplus.product.grid')) && ($_gridJsObject=$block->getJsObjectName())): ?>
    
<script type="text/javascript">
//<![CDATA[
var groupProducts = new $H({});

function groupProductRowClick(grid, event) {
	var trElement = Event.findElement(event, 'tr');
    var isInput   = Event.element(event).tagName == 'INPUT';
    if(trElement){
        var checkbox = Element.getElementsBySelector(trElement, 'input');
        if(checkbox[0]){
            var checked = isInput ? checkbox[0].checked : !checkbox[0].checked;
            <?php echo $_gridJsObject ?>.setCheckboxChecked(checkbox[0], checked);
        }
    }
}

function registerGroupProduct(grid, element, checked) {
	
	if (checked) {
		groupProducts.set(element.value, 1);
	} else {
		groupProducts.set(element.value, 0);
	}
	$('in_group_products').value = groupProducts.toQueryString();

}

function addProductToSet(id) {
	if (groupProducts.indexOf(id) < 0) {
		groupProducts[groupProducts.length] = id;
	}
}

function removeProductFromSet(id) {
	groupProducts = groupProducts.without(id);
}

function groupProductRowInit(grid, row) {
	var checkbox = $(row).getElementsByClassName('checkbox')[0];
	if (groupProducts.get(checkbox.value) == undefined) {
		if (checkbox.checked) groupProducts.set(checkbox.value, (checkbox.checked) ? 1 : 0);
	} else {
		checkbox.checked =  groupProducts.get(checkbox.value);
	}
}

<?php echo $_gridJsObject ?>.initRowCallback = groupProductRowInit;
<?php echo $_gridJsObject ?>.rowClickCallback = groupProductRowClick;
<?php echo $_gridJsObject ?>.checkboxCheckCallback = registerGroupProduct;
<?php echo $_gridJsObject ?>.rows.each(function(row){groupProductRowInit(<?php echo $_gridJsObject ?>, row)});

//]]>
</script>
<?php endif; ?>
